﻿/**********************************天使物流*******************************************
* 功能描述:合同管理-通用合同
* 作者:石锦涛
* 时间:2022-01-08
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.Contract.NewGeneralContras;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.Contract.NewGeneralContras
{
    public class NewGenerAppRepository : DapperDbContextRepository, INewGenerAppRepository
    {
        public NewGenerAppRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 通用合同显示
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<NewGeneralContraDto>> GetResponse(string telet, string danwei, string principal, string operators, string dtime, int? state, int pageindex = 1, int pagesize = 5)
        {
            var Sql = "select Id as GId,GeneralContractNos,Thecontracttitles,TheOtherUnits,TheOtherPartyHead,ContractCategory,ContractAmounts,Agents,SUBSTRING(CONVERT(Nvarchar(50), SignedTimes), 1, 10) as SignedTimes,TheContractAmount,ProjectDescriptios,ThisMainClauses,ContractTexts,ContractStatess,TheApprovers,IsDelete from NewGeneralContra where IsDelete=0 ";
            if (!string.IsNullOrWhiteSpace(telet))
            {
                Sql += $" and Thecontracttitles like '%{telet}%'";
            }
            if (!string.IsNullOrWhiteSpace(danwei))
            {
                Sql += $" and TheOtherUnits like '%{danwei}%'";
            }
            if (!string.IsNullOrWhiteSpace(principal))
            {
                Sql += $" and TheOtherPartyHead like '%{principal}%'";
            }
            if (!string.IsNullOrWhiteSpace(operators))
            {
                Sql += $" and Agents like '%{operators}%'";
            }
            if (dtime != null)
            {
                Sql += $" and SignedTimes ='{dtime}'";
            }
            if (state > 0)
            {
                Sql += $" and ContractStatess ='{state}'";
            }
            PageResponse<NewGeneralContraDto> doc = new PageResponse<NewGeneralContraDto>();
            var list = await this.QueryAsync<NewGeneralContraDto>(Sql);
            doc.PageTotal = list.Count();
            Sql += $" order by Id offset ({pageindex}-1)*{pagesize} rows fetch next {pagesize} rows only";
            var query = await this.QueryAsync<NewGeneralContraDto>(Sql, null);
            doc.PageData = query;
            return doc;

        }

        /// <summary>
        /// 通用逻辑删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<PageResponse<NewGeneralContraDto>> DeleteGeneral(int Id)
        {
            try
            {
                PageResponse<NewGeneralContraDto> doc = new PageResponse<NewGeneralContraDto>();

                var sql = $"update  NewGeneralContra set IsDelete=1 where Id={Id} ";
                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 通用修改
        /// </summary>
        /// <param name="contraDto"></param>
        /// <returns></returns>
        public async Task<PageResponse<int>> UpdateGeneral(NewGeneralContraDto contraDto)
        {
            try
            {
                PageResponse<int> doc = new PageResponse<int>();
                var sql = $"update NewGeneralContra set   GeneralContractNos='{contraDto.GeneralContractNos}',Thecontracttitles='{contraDto.Thecontracttitles}',TheOtherUnits='{contraDto.TheOtherUnits}',TheOtherPartyHead='{contraDto.TheOtherPartyHead}',ContractCategory='{contraDto.ContractCategory}',ContractAmounts = '{contraDto.ContractAmounts}',Agents = '{contraDto.Agents}',SignedTimes = '{contraDto.SignedTimes.ToString("yyyy/MM/dd")}',TheContractAmount = '{contraDto.TheContractAmount}',ProjectDescriptios = '{contraDto.ProjectDescriptios}',ThisMainClauses = '{contraDto.ThisMainClauses}',ContractTexts = '{contraDto.ContractTexts}',ContractStatess= '{contraDto.ContractStatess}',TheApprovers= '{contraDto.TheApprovers}',IsDelete= '{contraDto.IsDelete}' where Id = '{contraDto.GId}'";

                doc.Code = await this.ExecuteAsync(sql,null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 通用添加
        /// </summary>
        /// <param name="contraDto"></param>
        /// <returns></returns>
        public async Task<PageResponse<int>> PostGeneral(NewGeneralContraDto contraDto)
        {
            try
            {
                PageResponse<int> doc = new PageResponse<int>();
                //雪花算法
                var worker = new IdWorker(1, 1);
                var id = worker.NextId().ToString();

                var sql = $"insert NewGeneralContra values('{id}','{contraDto.Thecontracttitles}','{contraDto.TheOtherUnits}','{contraDto.TheOtherPartyHead}','{contraDto.ContractCategory}', '{contraDto.ContractAmounts}',  '{contraDto.Agents}', '{contraDto.SignedTimes.ToString("yyyy/MM/dd")}', '{contraDto.TheContractAmount}',  '{contraDto.ProjectDescriptios}',  '{contraDto.ThisMainClauses}', '{contraDto.ContractTexts}', '{contraDto.ContractStatess}', '{contraDto.TheApprovers}', '{contraDto.IsDelete}')";

                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 通用批量删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<PageResponse<NewGeneralContraDto>> DeletEmoreGeneral(string Id)
        {
            try
            {
                PageResponse<NewGeneralContraDto> doc = new PageResponse<NewGeneralContraDto>();
                var ids = Id.Split(',');
                foreach (var item in ids)
                {
                    var shu = Convert.ToInt32(item);
                    var list = await this.QueryAsync<NewGeneralContraDto>($"select * from NewGeneralContra where Id={shu}");
                    if(list!=null)
                    {
                        var sql = $"update NewGeneralContra set IsDelete=1 where Id={shu} ";
                        doc.Code = await this.ExecuteAsync(sql, null);
                    }
                }
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
